package com.example.xpvideo.service.serviceImpl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.xpvideo.entity.LsCarouselPicture;
import com.example.xpvideo.entity.ResponseResult;
import com.example.xpvideo.mapper.LsCarouselPictureMapper;
import com.example.xpvideo.service.FileService;
import com.example.xpvideo.service.LsCarouselPictureService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.multipart.MultipartFile;

/**
 *
 */
@Service
public class LsCarouselPictureServiceImpl extends ServiceImpl<LsCarouselPictureMapper, LsCarouselPicture>
    implements LsCarouselPictureService{

    @Autowired
    private LsCarouselPictureMapper carouselPictureMapper;
    @Autowired
    private FileService fileService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult addImages(MultipartFile file,Integer id) {
        String url = "";
        try {
             url = fileService.addImages(file);
            if (StringUtils.isEmpty(url)) {
                return ResponseResult.responseResult(500, "上传失败");
            }
            LsCarouselPicture carouselPicture = new LsCarouselPicture();
            carouselPicture.setImages(url);
            if (null != id) {
                carouselPicture.setId(id);
                carouselPictureMapper.updateById(carouselPicture);
            } else {
                carouselPictureMapper.insert(carouselPicture);
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResponseResult.responseResult(500, "上传失败");
        }
        return ResponseResult.responseResult(0,"添加成功");
    }

    /**
     * 删除
     * @param
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult delCarouselPicture(Integer id) {
        try {
            LsCarouselPicture carouselPicture = carouselPictureMapper.selectById(id);
            if (null != carouselPicture) {
                String imagesUrl = carouselPicture.getImages();
                boolean del=fileService.delImages(imagesUrl);
                if (del) {
                    carouselPictureMapper.deleteById(id);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResponseResult.responseResult(500, "删除失败");
        }
        return ResponseResult.responseResult(0,"删除成功");
    }
}




